#!/bin/sh
# vi: ts=4 noexpandtab

RESIZE_MODE="background"
GROWROOT="enabled"
if [ -f /etc/default/resizefs ]; then
	. /etc/default/resizefs
fi

case "$1" in
	start)
		lxc-is-container && exit 0
		[ -e /dev/root ] || exit 0
		case "$GROWROOT" in
			disabled|off|0) :;;
			*)
				growroot /dev/root ||
					{ echo "failed growing /dev/root"; exit 1; }
		esac
		# here we have to use start-stop-daemon or the resize
		# process gets killed when rc.sysinit ends (LP: #1261710)
		case "$RESIZE_MODE" in
			background)
				cirros-per once resize-rootfs -- \
					start-stop-daemon --start --background \
						--exec=/sbin/resize-filesystem -- \
						/dev/root /run/resize.rootfs /dev/console
				;;
			foreground)
				cirros-per once resize-rootfs -- \
						/sbin/resize-filesystem \
						/dev/root /run/resize.rootfs /dev/console
				;;
			disabled) : ;;
			*)
				echo "resizefs: unknown RESIZE_MODE='$RESIZE_MODE'." \
					"Doing nothing." 1>&2
				;;
		esac
		;;
	stop|restart|reload) : ;;
	*)
		echo $"Usage: $0 {start}"
		exit 1
		;;
esac

exit $?
